package payrechargelogic

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeChannelCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRechargeChannelCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeChannelCreateLogic {
	return &RechargeChannelCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RechargeChannelCreateLogic) RechargeChannelCreate(in *rpc.RechargeChannelCreateRequest) (*rpc.RechargeChannelCreateResponse, error) {
	var commonResp rpc.CommonResp

	// parameter check
	if in.Type == 0 {
		commonResp.Code = xerrors.ParameterError
		commonResp.Message = "invalid parameter"
		return &rpc.RechargeChannelCreateResponse{CommonResp: &commonResp}, errors.New("invalid parameter")
	}
	// data insertion
	channel := &schema.RechargeChannel{
		Type:         in.Type,
		MappingCode:  in.MappingCode,
		Name:         in.Name,
		Level:        in.Level,
		UserGroup:    in.UserGroup,
		BonusRate:    in.BonusRate,
		MinAmount:    in.MinAmount,
		MaxAmount:    in.MaxAmount,
		AmountConfig: in.AmountConfig,
		MerchantID:   in.MerchantId,
		AppId:        in.AppId,
		AppCode:      in.AppCode,
		SecretKey:    in.SecretKey,
		URL:          in.Url,
		ReturnUrl:    in.ReturnUrl,
		NotifyUrl:    in.NotifyUrl,
		PubKey:       in.PubKey,
		PrivateKey:   in.PrivateKey,
		StartTime:    in.StartTime,
		EndTime:      in.EndTime,
		Sort:         in.Sort,
		Status:       in.Status,
		IsTestable:   in.IsTestable,
		WhiteList:    in.WhiteList,
		Remark:       in.Remark,
		Icon:         in.Icon,
		ChannelName:  in.ChannelName,
		Coin:         in.Inr,
	}

	err := recharge.NewRechargeChannelModel(l.ctx, l.svcCtx.Db).RechargeChannelCreate(channel)
	if err != nil {
		commonResp.Code = xerrors.DBError
		commonResp.Message = "Database operation error"
		return &rpc.RechargeChannelCreateResponse{CommonResp: &commonResp}, err
	}

	commonResp.Message = "Create successful"
	return &rpc.RechargeChannelCreateResponse{CommonResp: &commonResp}, nil
}
